"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createBaseMongoClient = exports.BaseMongoClient = void 0;
const mongodb_1 = require("mongodb");
class BaseMongoClient {
    constructor(mongodb, db, collection) {
        this.mongodb = mongodb;
        this.db = db;
        this.collection = collection;
    }
    get col() {
        return this.mongodb.db(this.db).collection(this.collection);
    }
    insertMany(data) {
        return __awaiter(this, void 0, void 0, function* () {
            const { insertedIds } = yield this.col.insertMany(data);
            return insertedIds;
        });
    }
    insert(data) {
        return __awaiter(this, void 0, void 0, function* () {
            const { insertedIds } = yield this.col.insert(data);
            return insertedIds;
        });
    }
    createIndex(config) {
        return __awaiter(this, void 0, void 0, function* () {
            let indexName = config.indexName;
            let expireAfterSeconds = config.expireAfterSeconds;
            this.col.createIndex({ indexName: 1 }, { expireAfterSeconds: expireAfterSeconds });
        });
    }
    getOne(req) {
        return __awaiter(this, void 0, void 0, function* () {
            const { condition, projection = {} } = req;
            let res = yield this.col.findOne(condition, projection);
            return res;
        });
    }
    // async getOne(req: {condition:FilterQuery<T>,projection:object}) {
    //     let res = await this.col.findOne(req)
    //     return res;        
    // }
    getMany(req = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            const { sort, condition = {} } = req;
            const options = {};
            if (sort) {
                options.sort = { sort: 1 };
            }
            const res = yield this.col.find(condition, options).toArray();
            return res;
        });
    }
    getManyLimit(req = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            const { sort, limit = 100, skip = 0, condition = {} } = req;
            const options = {};
            if (sort) {
                options.sort = sort;
            }
            options.limit = limit;
            options.skip = skip * limit;
            // console.log(condition)
            const res = yield this.col.find(condition, options).toArray();
            return res;
        });
    }
    aggregate() {
        return __awaiter(this, void 0, void 0, function* () {
        });
    }
    updateOne(req) {
        return __awaiter(this, void 0, void 0, function* () {
            const { _id } = req;
            if (!_id) {
                throw new Error('_id is undefined');
            }
            delete req._id;
            yield this.col.updateOne({ _id }, { $set: req });
        });
    }
    getStats() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.col.stats();
        });
    }
    delete(req) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.col.deleteMany(req);
        });
    }
}
exports.BaseMongoClient = BaseMongoClient;
function createBaseMongoClient(config) {
    return __awaiter(this, void 0, void 0, function* () {
        const { db, uri, col } = config;
        const mongodb = yield mongodb_1.MongoClient.connect(uri, { useUnifiedTopology: true });
        const client = new BaseMongoClient(mongodb, db, col);
        return client;
    });
}
exports.createBaseMongoClient = createBaseMongoClient;
